import worker from '@ohos.worker';
import vibrator from '@ohos.vibrator';
import hilog from '@ohos.hilog';

const workerPort = worker.workerPort;

function logWithTimestamp(message: string): void {
  hilog.info(0x0000, 'shtest', `[${new Date().toISOString()}] ${message}`);
}
// 监听主线程发来的消息
workerPort.onmessage = async function(e) {
  console.log("Received message in worker:", e.data);

  let effect1: vibrator.VibrateTime = {
    type: 'time',
    duration: 100,
  }

  let effect2: vibrator.VibratePreset = {
    type: 'preset',
    effectId: 'effectId',
    count: 666,
    intensity: 200,
  }

  let effect3: vibrator.VibrateFromFile = {
    type: 'file',
    hapticFd: {
      fd: 1,
      offset: 200,
      length: 3,
    }
  }

  let effect5: vibrator.VibratePreset = {
    type: 'preset',
    effectId: 'effectId',
  }

  let attr: vibrator.VibrateAttribute = {
    id: 1,
    usage: 'alarm',
    systemUsage: true,
  }


  this.logWithTimestamp("==startVibration VibrateTime callback 1 - Before");
  let startTime = Date.now();
  vibrator.startVibration(effect1, attr, ((): void => {
    hilog.info(0x0000, 'shtest', "Callback: startVibration VibrateTime!");
  }));
  this.logWithTimestamp(`==startVibration VibrateTime callback 1 - After (${Date.now() - startTime}ms)`);

  this.logWithTimestamp("==startVibration VibratePreset callback 2 - Before");
  startTime = Date.now();
  vibrator.startVibration(effect2, attr, ((): void => {
    hilog.info(0x0000, 'shtest', "Callback: startVibration VibratePreset!");
  }));
  this.logWithTimestamp(`==startVibration VibratePreset callback 2 - After (${Date.now() - startTime}ms)`);

  this.logWithTimestamp("==startVibration VibratePreset callback 3 - Before");
  startTime = Date.now();
  vibrator.startVibration(effect5, attr, ((): void => {
    hilog.info(0x0000, 'shtest', "Callback: startVibration VibratePreset!");
  }));
  this.logWithTimestamp(`==startVibration VibratePreset callback 3 - After (${Date.now() - startTime}ms)`);

  this.logWithTimestamp("==startVibration VibrateFromFile callback 4 - Before");
  startTime = Date.now();
  vibrator.startVibration(effect3, attr, ((): void => {
    hilog.info(0x0000, 'shtest', "Callback: startVibration VibrateFromFile!");
  }));
  this.logWithTimestamp(`==startVibration VibrateFromFile callback 4 - After (${Date.now() - startTime}ms)`);

  this.logWithTimestamp("==startVibration VibrateTime promise - Before");
  startTime = Date.now();
  await vibrator.startVibration(effect1, attr).then<void, void>((): void => {
    hilog.info(0x0000, 'shtest', "VibrateTime promise");
  });
  this.logWithTimestamp(`==startVibration VibrateTime promise - After (${Date.now() - startTime}ms)`);

  this.logWithTimestamp("==startVibration VibratePreset promise - Before");
  startTime = Date.now();
  await vibrator.startVibration(effect2, attr).then<void, void>((): void => {
    hilog.info(0x0000, 'shtest', "VibratePreset promise");
  });
  this.logWithTimestamp(`==startVibration VibratePreset promise - After (${Date.now() - startTime}ms)`);

  this.logWithTimestamp("==startVibration VibrateFromFile promise - Before");
  startTime = Date.now();
  await vibrator.startVibration(effect3, attr).then<void, void>((): void => {
    hilog.info(0x0000, 'shtest', "VibrateFromFile promise");
  });
  this.logWithTimestamp(`==startVibration VibrateFromFile promise - After (${Date.now() - startTime}ms)`);

  this.logWithTimestamp("==isSupportEffect callback - Before");
  startTime = Date.now();
  vibrator.isSupportEffect("77", ((err: Error | undefined, value: boolean): void => {
    hilog.info(0x0000, 'shtest', "isSupportEffect callback value: " + value);
  }));
  this.logWithTimestamp(`==isSupportEffect callback - After (${Date.now() - startTime}ms)`);

  this.logWithTimestamp("==isSupportEffect promise - Before");
  startTime = Date.now();
  await vibrator.isSupportEffect("66").then<void, void>((value: boolean): void => {
    hilog.info(0x0000, 'shtest', "isSupportEffect promise value: " + value);
  });
  this.logWithTimestamp(`==isSupportEffect promise - After (${Date.now() - startTime}ms)`);

  for (let i = 0; i < 1000; i++) {
    hilog.info(0x0000, 'shtest', "test loop: " + i);
    vibrator.startVibration(effect1, attr, ((): void => {
      hilog.info(0x0000, 'shtest', "Callback: startVibration VibrateTime!");
    }));

    vibrator.startVibration(effect2, attr, ((): void => {
      hilog.info(0x0000, 'shtest', "Callback: startVibration VibratePreset!");
    }));

    vibrator.startVibration(effect5, attr, ((): void => {
      hilog.info(0x0000, 'shtest', "Callback: startVibration VibratePreset!");
    }));

    vibrator.startVibration(effect3, attr, ((): void => {
      hilog.info(0x0000, 'shtest', "Callback: startVibration VibrateFromFile!");
    }));

    await vibrator.startVibration(effect1, attr).then<void, void>((): void => {
      hilog.info(0x0000, 'shtest', "VibrateTime promise");
    });

    await vibrator.startVibration(effect2, attr).then<void, void>((): void => {
      hilog.info(0x0000, 'shtest', "VibratePreset promise");
    });

    await vibrator.startVibration(effect3, attr).then<void, void>((): void => {
      hilog.info(0x0000, 'shtest', "VibrateFromFile promise");
    });

    vibrator.isSupportEffect("77", ((err: Error | undefined, value: boolean): void => {
      hilog.info(0x0000, 'shtest', "isSupportEffect callback value: " + value);
    }));

    await vibrator.isSupportEffect("66").then<void, void>((value: boolean): void => {
      hilog.info(0x0000, 'shtest', "isSupportEffect promise value: " + value);
    });
  }
  workerPort.postMessage("Completed!"); // 将结果发送回主线程
};